package com.bst.etl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.bst.base.domain.BaseDimCompResult;
import com.bst.common.constant.JobConstant;
import com.bst.common.core.domain.AjaxResult;
import com.bst.etl.datax.entity.JobTemplateTask;
import com.bst.etl.service.ITableMetaQueryService;
import com.bst.etl.vo.EtlLabelDataVO;
import com.bst.etl.vo.TableFieldVO;
import com.bst.md.domain.MdMedPubfld;
import com.bst.md.domain.MdMedTb;
import com.bst.md.domain.MdMedTbFld;
import com.bst.md.service.IMdMedPubfldService;
import com.bst.system.service.ICommonTableDDLService;
import com.bst.system.vo.AlterTableParamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bst.etl.mapper.EtlDrawDwMapper;
import com.bst.etl.domain.EtlDrawDw;
import com.bst.etl.service.IEtlDrawDwService;
import com.bst.common.core.text.Convert;

/**
 * 标准仓库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-28
 */
@Service
public class EtlDrawDwServiceImpl implements IEtlDrawDwService 
{
    @Autowired
    private EtlDrawDwMapper etlDrawDwMapper;
    @Autowired
    ITableMetaQueryService tableMetaQueryService;
    @Autowired
    IMdMedPubfldService mdMedPubfldService;
    @Autowired
    ICommonTableDDLService commonTableDDLService;

    /**
     * 查询标准仓库
     * 
     * @param idDw 标准仓库主键
     * @return 标准仓库
     */
    @Override
    public EtlDrawDw selectEtlDrawDwByIdDw(Long idDw)
    {
        return etlDrawDwMapper.selectEtlDrawDwByIdDw(idDw);
    }

    /**
     * 查询标准仓库列表
     * 
     * @param etlDrawDw 标准仓库
     * @return 标准仓库
     */
    @Override
    public List<EtlDrawDw> selectEtlDrawDwList(EtlDrawDw etlDrawDw)
    {
        return etlDrawDwMapper.selectEtlDrawDwList(etlDrawDw);
    }

    @Override
    public List<EtlDrawDw> selectEtlDrawDwList2(EtlLabelDataVO dataVO) {
        return etlDrawDwMapper.selectEtlDrawDwList2(dataVO);
    }

    /**
     * 新增标准仓库
     * 
     * @param etlDrawDw 标准仓库
     * @return 结果
     */
    @Override
    public int insertEtlDrawDw(EtlDrawDw etlDrawDw)
    {
        etlDrawDw.setFgAct(1);
        etlDrawDw.setFgTbCre(0);
        etlDrawDw.setDtSysCre(new Date());
        etlDrawDw.setSqlInsert(getInsertSql(etlDrawDw));
        return etlDrawDwMapper.insertEtlDrawDw(etlDrawDw);
    }

    /**
     * 修改标准仓库
     * 
     * @param etlDrawDw 标准仓库
     * @return 结果
     */
    @Override
    public int updateEtlDrawDw(EtlDrawDw etlDrawDw)
    {
        etlDrawDw.setDtSysModi(new Date());
        etlDrawDw.setSqlInsert(getInsertSql(etlDrawDw));
        return etlDrawDwMapper.updateEtlDrawDw(etlDrawDw);
    }

    /**
     * 批量删除标准仓库
     * 
     * @param idDws 需要删除的标准仓库主键
     * @return 结果
     */
    @Override
    public int deleteEtlDrawDwByIdDws(String idDws)
    {
        return etlDrawDwMapper.deleteEtlDrawDwByIdDws(Convert.toStrArray(idDws));
    }

    /**
     * 删除标准仓库信息
     * 
     * @param idDw 标准仓库主键
     * @return 结果
     */
    @Override
    public int deleteEtlDrawDwByIdDw(Long idDw)
    {
        return etlDrawDwMapper.deleteEtlDrawDwByIdDw(idDw);
    }

    @Override
    public List<EtlDrawDw> selectEtlDrawDwByIdTemplate(Integer idTmplate) {
        return etlDrawDwMapper.selectEtlDrawDwByIdTemplate(idTmplate);
    }

    @Override
    public AjaxResult dwSQlValid(String sql) {
        List<TableFieldVO> list = tableMetaQueryService.findAllFldsBySql(sql,"dw");
        List<String> fields = list.stream().map(TableFieldVO::getName).collect(Collectors.toList());
        List<MdMedPubfld> pubflds =mdMedPubfldService.findByIdList(fields);
        Map<String,MdMedPubfld> pubFields = pubflds.stream().collect(Collectors.toMap(MdMedPubfld::getIdPubfld, e->e));
        List<MdMedPubfld> newPubfld = new ArrayList<>();
        if(pubflds.size()==fields.size()) {
            for (String field : fields) {
                newPubfld.add(pubFields.get(field));
            }
            return AjaxResult.success(newPubfld);
        } else {
            String errFlds = "";
            for (String field : fields) {
                if(!pubFields.keySet().contains(field)) {
                    errFlds += field+",";
                }
            }
            return AjaxResult.error("以下字段非法，请完善["+errFlds+"]");
        }
    }

    @Override
    public AjaxResult createDwTable(Long idDw) {
        EtlDrawDw dw = etlDrawDwMapper.selectEtlDrawDwByIdDw(idDw);
        String sql=dw.getSqlQry();
        AjaxResult result = dwSQlValid(sql);
        if(AjaxResult.isSuccess(result)) {
            List<MdMedPubfld> pubflds = (List<MdMedPubfld>)result.get(AjaxResult.DATA_TAG);
            MdMedTb mdMedTb = new MdMedTb();
            mdMedTb.setCd(dw.getCdTb());
            mdMedTb.setNa(dw.getNa());
            mdMedTb.setSdTbca(JobConstant.SD_TBCA_DW);
            List<MdMedTbFld> mdMedTbFlds = new ArrayList();
            MdMedTbFld tbFld;
            for (MdMedPubfld pubfld : pubflds) {
                tbFld = new MdMedTbFld();
                tbFld.setIdPubfld(pubfld.getIdPubfld());
                mdMedTbFlds.add(tbFld);
            }
            result = commonTableDDLService.alterTableByMed(new AlterTableParamVO(mdMedTb, mdMedTbFlds,(dw.getFgTbCre()==0?null:dw.getCdTb())));
            if(AjaxResult.isSuccess(result)) {
                dw.setFgTbCre(1);
                this.updateEtlDrawDw(dw);
            }
        }
        return result;
    }

    private String getInsertSql(EtlDrawDw dw) {
        AjaxResult result = dwSQlValid(dw.getSqlQry());
        List<MdMedPubfld> pubflds = (List<MdMedPubfld>)result.get(AjaxResult.DATA_TAG);
        String insertSql="insert into "+dw.getCdTb()+"(";
        String values = " values (";
        for (MdMedPubfld pubfld : pubflds) {
            if(pubfld==null) {
                continue;
            }
            insertSql += pubfld.getIdPubfld()+",";
            values += "?,";
        }
        insertSql = insertSql.substring(0,insertSql.length()-1)+") ";
        values = values.substring(0,values.length()-1)+") ";
        return insertSql+values;
    }
}
